<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表数据 -->
      <el-table :data="orderlist" border stripe>
        <!-- 在表格中的-column子组件通过prop使用对应的属性,而非表格子组件使用,需要通过作用域(防止对全局造成影响)插槽的方式使用 -->
        <!-- vue slot的scope传递值是父作用域中的源数据改变，值会同步改变。获取当前行数据,必须使用.row内置属性获取 -->
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showBox(scope.row)"
            ></el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location"
              @click="showAddress(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 物流信息查询对话框 -->
    <!-- <el-dialog title="" :visible.sync="progressVisible" width="50%">
      <el-radio-group>
        <el-radio label="label"></el-radio>
      </el-radio-group>
      <div slot="footer">
        <el-button @click="progressVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog> -->

    <!-- 修改订单状态对话框 -->
    <el-dialog title="修改订单" width="50%" :visible.sync="orderVisible"
    @close="orderDialogClosed">
      <el-form ref="orderFormRef" :model="orderForm" label-width="120px" :rules="orderRules">
        <el-form-item label="订单是否发货" prop="is_send">
          <el-select placeholder="请选择是否发货" v-model="orderForm.is_send">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单支付" prop="order_pay">
          <el-select placeholder="请选择订单支付方式" v-model="orderForm.order_pay">
            <el-option label="未支付" value="0"></el-option>
            <el-option label="支付宝" value="1"></el-option>
            <el-option label="微信" value="2"></el-option>
            <el-option label="银行卡" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单价格" prop="order_price">
          <el-input v-model="orderForm.order_price" style="width:217px"></el-input>
        </el-form-item>
        <!-- <el-form-item label="订单数量">
          <el-input v-model="orderForm.order_number"></el-input>
        </el-form-item> -->
        <el-form-item label="支付状态" prop="pay_status">
          <el-select placeholder="请选择支付状态" v-model="orderForm.pay_status">
            <el-option label="已付款" value="1"></el-option>
            <el-option label="未付款" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="orderVisible = false">取 消</el-button>
        <el-button type="primary" @click="orderConfirm"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        ref="addressFormRef"
        :model="consigneeAddress"
        :rules="addressRules"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="consigneeAddress.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="consigneeAddress.address2"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'

export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      orderlist: [],
      progressVisible: false,
      progressInfo: [],
      addressVisible: false,
      consigneeAddress: {
        address1: [],
        address2: '',
      },
      addressRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData,
      orderVisible: false,
      orderForm: {},
      addressForm: {},
      orderRules:{
        order_price:[
        {required: true, message:'订单价格不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    //获取订单列表数据
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败!')
      }
      this.total = res.data.total
      this.orderlist = res.data.goods
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    //展示物流进度的对话框
    // async showProgress() {
    //   // this.progressVisible = true
    // },
    //展示修改订单的对话框
    showBox(row) {
      this.orderForm = {
        order_id: row.order_id,
        is_send: row.is_send,
        order_pay: row.order_pay,
        order_price: row.order_price,
        pay_status: row.pay_status
      }
      // this.orderForm=row
      this.orderVisible = true
    },
    orderDialogClosed(){
      //重置为空,否则提交后造成该变量数据冲突
      this.orderForm={}
      //会缓存变量的第一次数据作为重置的标准
      this.$refs.orderFormRef.resetFields()
    },
    orderConfirm(){
      this.$refs.orderFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写必填的选项')
        const {data:res} =await this.$http.put('orders/'+this.orderForm.order_id,this.orderForm)
        if (res.meta.status !== 200){
          return this.$message.error('修改订单状态失败')
        }
          this.orderVisible = false
          this.getOrdersList()
          this.$message.success('修改订单状态成功')
      })
    },
    
    showAddress(row){
      this.addressForm=row
      this.consigneeAddress = JSON.parse(row.consignee_addr)
      this.addressVisible=true
    },
    addressDialogClosed() {
      this.consigneeAddress = {}
      this.$refs.addressFormRef.resetFields()
    },
    addressConfirm() {
      this.$refs.addressFormRef.validate(async (valid) => {
        if(!valid) return this.$message.error('请填写必要的表单项')
        this.addressForm.consignee_addr = JSON.stringify(this.consigneeAddress)
        //向后端发起修改订单地址的请求
        const { data: res } = await this.$http.put(
          'orders/address/' + this.addressForm.order_id,
          this.addressForm
        )
        if (res.meta.status === 200) {
          this.$message.success('修改地址成功')
        } else {
          this.$message.error('修改地址失败')
        }
        this.getOrdersList()
        this.addressVisible = false
      })
    },
  },
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100% !important;
}
</style>
